<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />

<link rel="shortcut icon" href="/favicon.ico"
	type="image/vnd.microsoft.icon" />
<title>$lang.get('browser.title')</title>
<link href='/css/giiwa.css' rel='stylesheet' />
<link href='/css/webshell.css' rel='stylesheet' />

<script type="text/javascript" src="/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="/js/ajaxfileupload.js"></script>

</head>
<body onresize='_resize();'>
	<div id='wrapper'>
		<div id='console' >
			<div class='center'>
			</div>
		</div>
		<div id='cmdline'>
			<div class='center'>
				<input type='text' name='line' value='' onkeypress='_keypress(event)'/>
				<a class="send btn btn-info" onclick='_send()'>发送</a>
				<span class='upload'>
					<a class="btn btn-success" href='javascript:;' class='btn'>上传</a>
					<input id='uploadfile' type='file' name='file' onchange='_upload();'/>
				</span>
			</div>
		</div>
	</div>

<iframe id="download-iframe" style='display:none'></iframe>
	
<script>
function _upload() {
	var file = $('#cmdline input[name=file]').val();
	 if(file){
		_append('upload ' + file, 'input');
		
		//uploadfile
		$.ajaxFileUpload({
			url:'/shell/upload',
			type: 'post',
			secureuri:false,
			fileElementId:'uploadfile',
			dataType: 'json',
			success: function (d, status){
				if(d.message){
					_append(d.message, 'message');
				} else if(d.error) {
					_append(d.error, 'error');
				}
			},
			error: function (data, status, e){
				_append(e, 'error');
			}
		});			 
   }		
}

function getKey( event )
{
    if (window.event)
    {
        return window.event.keyCode;
    }
    else if (event)
    {
        return event.which;
    }
    else
    {
        return null;
    }
}

function _keypress(e){
	var key = getKey( e );
    if (key == null) return true;

    if ( key==13 )
    {
        _send();
        return false;
    }
}
    
function _send(){
	var p = {};
	var e = $('#cmdline input[name=line]');
	p.line = e.val();
	if(p.line.length > 0) {
		e.val('');
		e.focus();
		_append(p.line, 'input');

		var download=new RegExp("^download "); 
		
		if(p.line == 'exit' || p.line == 'quit') {
			window.close();
		} else if(download.test(p.line)){
			$('#download-iframe').attr('src', '/shell/download/' + p.line.substring(9));
		} else {
			$.post('/shell/run', p, function(d){
				if(d.status == 200 && d.message) {
					_append(d.message, 'message');
				} else if(d.status == 201 && d.message){
					_append(d.message, 'error');				
				}
			});
		}
	}else {
		_append('', 'input');
	}
}

function _append(s, t) {
	if(t == 'input') {
		s = '<p class="input">$&gt;' + s + '</p>';
	} else if(t == 'message') {
		s = '<p class="message">' + s + '</p><br/>';
	} else if(t == 'error') {
		s = '<p class="error">' + s + '</p><br/>';
	}
	
	var e = $('#console .center');
	e.append(s);
	e.scrollTop(e[0].scrollHeight);
	
}
function _resize() {
	var h = $('body').height();
	$('#console').css('height', (h - 55) + 'px');
}

$(function(){
	$('body').trigger('resize');
	
	$('#cmdline input[name=line]').focus();
})

</script>	
</body>
</html>
